#include "Parser.h"

FileParser::FileParser()
{
	memset(this->FileName, 0, MAX_FILE_NAME_SIZE);
}

FileParser::FileParser(const char *FileName)
{
	strcpy_s(this->FileName, MAX_FILE_NAME_SIZE, FileName);
}

FileParser::~FileParser() {}

void FileParser::NewFile(const char *FileName)
{
	strcpy_s(this->FileName, MAX_FILE_NAME_SIZE, FileName);
}

int FileParser::ParseFile(void)
{
	if (this->FileName[0] == 0)
	{
		return 0;
	}

	char TempBuffer[DATA_BUFFER_SIZE];
	char VolatileData[DATA_BUFFER_SIZE];
	DataNode TempNode;
	std::string StringBuffer;
	std::ifstream MainFile(FileName);

	if (!MainFile.good())
	{
		return 0;
	}

	while (!MainFile.eof())
	{
		MainFile >> TempBuffer;
		std::ifstream FileStream(TempBuffer);

		if (!FileStream.good())
		{
			return 0;
		}

		// Take the first line of the file, this should hold the name of the object
		FileStream >> TempBuffer;
		FileStream >> TempBuffer;
		strcpy_s(TempNode.Name, strlen(TempBuffer) + 1, TempBuffer);

		// Parse the rest of the file, getting data names and values
		while (!FileStream.eof())
		{
			// Grab the next line from the file, two parts, TempBuffer holds the data name and VolatileData holds the value
			FileStream >> StringBuffer; // Silly standard strings :(
			FileStream >> VolatileData;

			TempNode.DataParameters[ StringBuffer ] = (float)atof(VolatileData);
		}

		Data.push_back(TempNode);
		FileStream.close();
	}

	MainFile.close();
	
	return 1;
}
